﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using WPFChatClient;
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace WPFChatClient
{
    public partial class ForumWindow : Window
    {
        private const String FormTitle = @"Forum: ";
        private const String StatusTitle = @"Connection Status: ";
        private MessageClient client;
        private String windowName;
        private ArrayList allowedUserList;
        private String userString;
        public List<CheckedListItem> currentUserList;


        private char SPLIT_CHAR = (char)23;

        public ForumWindow(MainWindow mainWindow, String name)
        {
            allowedUserList = new ArrayList();
            currentUserList = new List<CheckedListItem>();
            currentUserList.Add(new CheckedListItem("SHIT"));
            this.client = mainWindow.getMessageClient();
            this.windowName = name;
            InitializeComponent();
            this.Title = "JCU Forum";

        }

        /**
         * Adds a line of text to the message area.
         * Called using a delegate event in the MessageClient class.
         */
        public void client_updateMessageArea(string message)
        {
            Dispatcher.Invoke((Action)delegate
            {
                PostTitle.Text += message + "\n";
            });
        }

        public String getName()
        {
            return windowName;
        }

        /**
         * Get a comma-separated variable list of all users that are allowed to see the post.
         */
        private String getAllowedUserString()
        {
            String allowedUserString = "";
            foreach (CheckedListItem c in currentUserList)
            {
                if (c.IsChecked)
                {
                    allowedUserString += c.Name + ",";
                }
            }

            // Remove trailing comma.
            allowedUserString = allowedUserString.Substring(0, allowedUserString.Length - 1);
            return allowedUserString;
        }

        /**
         * 
         */
        private void transmitMessage(String newMessage)
        {
            if (newMessage.Length == 0)
                return;

            //newMessage = client.formatForumMessage(windowName, newMessage);
            client.sendThrottledMessage(newMessage);
        }


        public void appendChatLine(String str)
        {
            Dispatcher.Invoke((Action)delegate
            {
                PostTitle.AppendText(str + "\n");
            });
        }

        /**
         * Clear the text from the message area.
         */
        public void clearMessageScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispatcher.Invoke((Action)delegate
            {
                PostTitle.Clear();
            });
        }


        internal void updateUserList(string[] newUserList)
        {
            Dispatcher.Invoke((Action)delegate
            {
                userListBox.IsEnabled = true;
                currentUserList.Clear();
                foreach (string s in newUserList)
                {
                    currentUserList.Add(new CheckedListItem(s));
                }
                userListBox.ItemsSource = currentUserList;
                userListBox.UpdateLayout();
            });
        }

        public class CheckedListItem
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public bool IsChecked { get; set; }

            public CheckedListItem(String name)
            {
                this.Name = name;
                this.IsChecked = false;
            }
        }

        private void NewPostBtn_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.Invoke((Action)delegate
            {
                String title = PostTitle.Text;
                String newPost = client.formatForumMessage(title, getAllowedUserString(), windowName, messageArea.Text);
                transmitMessage("POST" + SPLIT_CHAR + newPost);
                PostTitle.Clear();
                this.Close();
            });

        }
    }
}
